package gdoku;

import gdoku.generator.Generator;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import kameleon.document.Document;
import kameleon.exception.KameleonException;
import kameleon.plugin.SupportedOptions;
import kameleon.util.IODocument;
import kameleon.util.IOFile;

/**
 * Main class.
 * 
 * <p>Launches the generation for the given file and 
 * writes the result to the given file.
 * 
 * <p>Proper usage requires three command line arguments : 
 * <pre>  -g &lt;analyzed file result> &lt;result file></pre>
 * 
 * @author		Dervin Cyrielle, Schnell Michaël
 * @version		1.0
 */
public class Main extends kameleon.plugin.Generator {

	/**
	 * Main function.
	 * 
	 * @param 	args
	 * 			command line arguments
	 * 
	 * @see		Main#checkArgs(String[])
	 */
	public static void main(String[] args) {
		new Main(args) ;
	}// main(String[])

	public Main() {
		super() ;
	}

	/**
	 * Sole constructor.
	 * 
	 * @param 	args
	 * 			command line arguments
	 * 
	 * @see		Main#checkArgs(String[])
	 */
	public Main(String[] args) {
		super() ;
		try {
			/* Check and retrieve arguments */
			Main.checkArgs(args) ;
			String documentPath = args[1] ;
			String outputFile   = args[2] ;

			/* Read Document from file */
			System.out.printf("~~ DokuWiki Generator ~~\n") ;
			System.out.printf("Generating file '%s' ...\n", args[2]) ;
			Document source = IODocument.readFromFile(documentPath) ;

			/* Generate result file */
			Generator g = new Generator(new File(outputFile), source) ;
			g.generate() ;
			g.close() ;
			System.out.printf("File successfully generated.\n") ;
		} catch (KameleonException e) {
			System.exit(1) ;
		}// try
	}// Main(String, String)

	/**
	 * Checks the given command line arguments.
	 * 
	 * <p>The program needs 3 arguments to run properly :
	 * <ol>
	 * 	<li>{@code -g} : analyze the given file
	 * 	<li>{@code <analyzed file result>} : path of the file containing an 
	 * 		instance of {@code Document}
	 * 	<li>{@code <result file>} : path of the target file for the result
	 * 
	 * @param 	args
	 * 			command line arguments
	 * 
	 * @throws	KameleonException
	 * 			If the number of arugments is incorrect
	 */
	protected static void checkArgs(String[] args) throws KameleonException {
		if (args.length != 3) {
			throw new KameleonException(
					String.format("Nombre d'arguments incorrect, %d trouves, %d attendus.\n",
							args.length, 3)) ;
		}// if
	}// checkArgs(String[])

	@Override
	public void generate(Document base, String filePath,
			SupportedOptions options) throws KameleonException {
		Generator g = new Generator(new File(filePath), base) ;
		g.generate() ;
		g.close() ;
	}

	@Override
	public void generate(Document base, String filePath) throws KameleonException {
		//TODO Temporary - replace with default options
		this.generate(base, filePath, null) ;
	}

	@Override
	public void copyPicture(String targetFolder) throws KameleonException {
		String[] pictureNames = new String[]{
				"DokuWikiGenerator_mini.png", 
				"DokuWikiGenerator_gray_icon.png",
				"DokuWikiGenerator_icon.png"
		} ;
		for(String pictureName : pictureNames) {
			try {
				InputStream src = new BufferedInputStream(
						this.getClass().getResourceAsStream(
								String.format("/gdoku/resources/%s", pictureName))) ; //$NON-NLS-1$
				OutputStream dest = new BufferedOutputStream(new FileOutputStream(String.format("%s%s%s", 
						targetFolder, File.separator, pictureName))) ;
				IOFile.copyFile(src, dest) ;
				src.close() ;
				dest.close() ;
			} catch (FileNotFoundException e) {
				//TODO Handle exception
				throw new KameleonException(e.getMessage()) ;
			} catch (IOException e) {
				throw new KameleonException(e.getMessage()) ;
			}// try
		}// for
	}

}// class Main